<?php

namespace Apps\Manage\Controllers;


use Phalcon\Mvc\View;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;
use Apps\Common\Libs\UploadPic;

class ToolsController extends BaseController
{
    /**
     * 地图选择和显示
     */
    public function mapAction()
    {
        $req = $this->request;
        $this->view->setVar('coordinate', $req->getQuery('coordinate', null, ''));
        $this->view->setVar('callback', $req->getQuery('callback', null, ''));
        $this->view->setVar('type', $req->getQuery('type', null, ''));
    }

    /**
     * 图文编辑器
     */
    public function editorPTAction()
    {
        $req = $this->request;
        $type = $req->getQuery('type', null, '');
        if(empty($type)) {
            $this->view->disable();
            echo 'type参数为空！';
            return;
        }
        if(in_array($type, ['bpService', 'activity']) == false) {
            $this->view->disable();
            echo 'type参数不支持！';
            return;
        }
        $this->view->setVar('type', $type);
    }

    public function editorPTUploadImgAction()
    {
        $req = $this->request;
        if(!$req->isPost()) {
            $this->view->setVar('callback', $req->getQuery('callback', null, 'parent.setFormResultImg'));
            $this->view->setVar('type', $req->getQuery('type', null, ''));
            return;
        }
        $response = new ResponseResult();
        $response->callback = $req->getPost('callback', null, '');
        $response->callbackJavascriptTag = true;
        $contentType = $req->getPost('content_type', null, '');
        if(in_array($contentType, ['bpService', 'activity', 'banner']) == false) {
            $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, 'type参数不支持');
            return $response;
        }
        $upload = new UploadPic();
        $upload->request = $req;
        switch($contentType) {
            case 'bpService':
                $pics = $upload->uploadPicByFile('file', 'beauty_parlor_pics');
                if($pics && count($pics) > 0) {
                    $response->sendResult($pics[0]);
                } else {
                    if($_FILES['file']['error'] == 1) {
                        $response->sendError(ResponseResultStatus::UPLOAD_FILE_ERROR, '文件大小超过了1M！');
                    } else {
                        $response->sendError(ResponseResultStatus::UPLOAD_FILE_ERROR, '文件处理异常！');
                    }

                }
                break;
            case 'activity':
                $pics = $upload->uploadPicByFile('file', 'activity_pics');
                if($pics && count($pics) > 0) {
                    $response->sendResult($pics[0]);
                } else {
                    if($_FILES['file']['error'] == 1) {
                        $response->sendError(ResponseResultStatus::UPLOAD_FILE_ERROR, '文件大小超过了1M！');
                    } else {
                        $response->sendError(ResponseResultStatus::UPLOAD_FILE_ERROR, '文件处理异常！');
                    }
                }
                break;
            case 'banner':
                $pics = $upload->uploadPicByFile('file', 'banner_pics');
                if($pics && count($pics) > 0) {
                    $response->sendResult($pics[0]);
                } else {
                    if($_FILES['file']['error'] == 1) {
                        $response->sendError(ResponseResultStatus::UPLOAD_FILE_ERROR, '文件大小超过了1M！');
                    } else {
                        $response->sendError(ResponseResultStatus::UPLOAD_FILE_ERROR, '文件处理异常！');
                    }
                }
                break;
        }
        return $response;
    }
}